﻿using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
using AntDesign;
using System.Text.RegularExpressions;
using System.Net.Http;
using System;
using System.Threading.Tasks;
using System.Net.Http.Json;

namespace ImoviesBlazorAntPro.Client.Pages.Admin
{
    public partial class Collect
    {
        [Inject]
        HttpClient HttpClient { get; set; }
        [Inject]
        IMessageService MessageService { get; set; }

        /// <summary>
        /// 被选中的视频分类
        /// </summary>
        private string _videoCate;

        /// <summary>
        /// 全部分类
        /// </summary>
        private List<string> cates = new List<string>
        {
            "电视剧","电影","动漫"
        };

        /// <summary>
        /// 输入网址
        /// </summary>
        private string _url;

        /// <summary>
        /// 电视剧采集开始按钮是否禁用
        /// </summary>
        private bool _teleplayStartDisable = false;
        /// <summary>
        /// 电影采集开始按钮是否禁用
        /// </summary>
        private bool _movieStartDisable = false;
        /// <summary>
        /// 动漫采集开始按钮是否禁用
        /// </summary>
        private bool _animationStartDisable = false;

        /// <summary>
        /// 电视剧采集列表
        /// </summary>
        private List<string> teleplayAwaitList = new List<string>();
        /// <summary>
        /// 电影采集列表
        /// </summary>
        private List<string> movieAwaitList = new List<string>();
        /// <summary>
        /// 动漫
        /// </summary>
        private List<string> animationAwaitList = new List<string>();

        private bool _teleplayListLoading = false;
        private bool _movieListLoading = false;
        private bool _animationListLoading = false;

        /// <summary>
        /// 添加网址到列表
        /// </summary>
        private void AddLink()
        {
            if (string.IsNullOrEmpty(_videoCate))
            {
                MessageService.Warning("请选择类型", 1.5);
                return;
            }
            if (string.IsNullOrEmpty(_url))
            {
                MessageService.Warning("请输入网址", 1.5);
                return;
            }
            if (!Regex.IsMatch(_url, @"(http|https)://.*"))
            {
                MessageService.Warning("请输入正确网址", 1.5);
                return;
            }
            switch (_videoCate)
            {
                case "电视剧":
                    teleplayAwaitList.Add(_url);
                    _url = string.Empty;
                    break;
                case "电影":
                    movieAwaitList.Add(_url);
                    _url = string.Empty;
                    break;
                case "动漫":
                    animationAwaitList.Add(_url);
                    _url = string.Empty;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 删除电视剧链接
        /// </summary>
        /// <param name="link"></param>
        private void RemoveTeleplayLink(string link)
        {
            teleplayAwaitList.Remove(link);
        }

        /// <summary>
        /// 删除电影链接
        /// </summary>
        /// <param name="link"></param>
        private void RemoveMovieLink(string link)
        {
            movieAwaitList.Remove(link);
        }


        /// <summary>
        /// 删除动漫链接
        /// </summary>
        /// <param name="link"></param>
        private void RemoveAnimationLink(string link)
        {
            animationAwaitList.Remove(link);
        }


        /// <summary>
        /// 开始电视剧采集
        /// </summary>
        private async Task StartTeleplayCollectAsync()
        {
            if (teleplayAwaitList.Count < 1) return;
            _teleplayStartDisable = true;
            _teleplayListLoading = true;
            // 进行采集请求
            var res = await HttpClient.PostAsJsonAsync("/api/collect/teleplay", teleplayAwaitList).WaitAsync(TimeSpan.FromSeconds(15));
            if (!res.IsSuccessStatusCode)
            {
                MessageService.Error("出错啦");
                return;
            }
            _teleplayListLoading = false;
            teleplayAwaitList.Clear();
            _teleplayStartDisable = false;
        }

        /// <summary>
        /// 开始电影采集
        /// </summary>
        private async Task StartMovieCollectAsync()
        {
            if (movieAwaitList.Count < 1) return;
            _movieStartDisable = true;
            _movieListLoading = true;
            // 进行采集请求
            var res = await HttpClient.PostAsJsonAsync("/api/collect/movie", movieAwaitList).WaitAsync(TimeSpan.FromSeconds(15));
            if (!res.IsSuccessStatusCode)
            {
                MessageService.Error("出错啦");
                return;
            }
            _movieListLoading = false;
            movieAwaitList.Clear();
            _movieStartDisable = false;
        }

        /// <summary>
        /// 开始动漫采集
        /// </summary>
        private async Task StartAnimationCollect()
        {
            if (animationAwaitList.Count < 1) return;
            _animationStartDisable = true;
            _animationListLoading = true;
            // 进行采集请求
            var res = await HttpClient.PostAsJsonAsync("/api/collect/animation", animationAwaitList).WaitAsync(TimeSpan.FromSeconds(15));
            if (!res.IsSuccessStatusCode)
            {
                MessageService.Error("出错啦");
                return;
            }
            _animationListLoading = false;
            animationAwaitList.Clear();
            _animationStartDisable = false;
        }
    }
}
